package com.cse.services;

import java.util.List;

import com.cse.engine.EngineFactory;
import com.cse.engine.subEngine.Engine;
import com.cse.mvc.models.Result;
import com.cse.utils.AsyncCommunicationsInterface;
import com.mutear.ParallelSearcher.utils.IResult;

/**
 * 每个搜索引擎的搜索线程
 * @author Mutear
 *
 */
public class SearchThread extends Thread {
	private AsyncCommunicationsInterface asyncCommunicationsInterface;
	private String engineName;
	private String key;

	public SearchThread(
			AsyncCommunicationsInterface asyncCommunicationsInterface,
			String engineName, String key) {
		this.asyncCommunicationsInterface = asyncCommunicationsInterface;
		this.engineName = engineName;
		this.key = key;
	}

	@Override
	public void run() {
		EngineFactory engineFactory = EngineFactory.getEngineFactory();
		Engine engine = engineFactory.createEngine(engineName);
		List<IResult> result = engine.search(key);
		// 由于排序需要的数据结构为数组
		// 因此需要在传递数据前将 List 转换为数组
		IResult[] data = new IResult[result.size()];
		for(int i = 0; i < result.size(); i++){
			data[i] = result.get(i);
		}
		asyncCommunicationsInterface.transferData(data);
	}
}